using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using DataObject.Database;
using DataObject.Core;
using DataObject.Helpers;

namespace DataObject.DataAccess
{
    public partial class StickyDAO : DataBaseProcessBase<StickyEntity, OBSDataContext>
    {
        public static StickyDAO CreateInstant()
        {
            return new StickyDAO();
        }

        public IList<StickyEntity> GetStickies(System.DateTime fromDate, System.DateTime toDate)
        {
            IList<StickyEntity> result = null;

            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from q in db.StickyEntities
                             where q.StickyDate >= fromDate.Date &&
                             q.StickyDate < toDate.Date.AddDays(1)
                             select q);
                if (query != null)
                    result = query.ToList();
            }

            return result;
        }

        public bool Create(StickyEntity item)
        {
            bool result = false;
            try
            {
                using (OBSDataContext db = new OBSDataContext(base.connectionString))
                {
                    var query = (from q in db.StickyEntities
                                 where q.StickyDate.Date == item.StickyDate.Date
                                 select q).SingleOrDefault();
                    if (query == null)
                    {
                        item.StickyID = Helper.NewSeqGuid();
                        db.StickyEntities.InsertOnSubmit(item);
                        db.SubmitChanges();
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
            }
            catch (Exception)
            {

            }

            return result;
        }

        public void Update(StickyEntity item)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from q in db.StickyEntities
                             where q.StickyDate.Date == item.StickyDate.Date
                                 select q).SingleOrDefault();
                if (query != null)
                {
                    query.Description = item.Description;
                    db.SubmitChanges();
                }
            }
        }

        public void Delete(DateTime stickyDate)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from q in db.StickyEntities
                             where q.StickyDate.Date == stickyDate.Date
                             select q).SingleOrDefault();
                if (query != null)
                {
                    db.StickyEntities.DeleteOnSubmit(query);
                    db.SubmitChanges();
                }
            }
        }

        #region Extensibility Method Declarations
        public void InsertCollection(List<StickyEntity> ls)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.StickyEntities.InsertAllOnSubmit(ls);
                db.SubmitChanges();
            }
        }

        public List<StickyEntity> GetBoatAll()
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.StickyEntities.ToList();
            }
        }
        public StickyEntity GetStickyByDate(DateTime pStickyDate)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.StickyEntities.SingleOrDefault(a => a.StickyDate.Equals(pStickyDate.Date));
            }
        }
        public List<StickyEntity> SelectListItem(Guid StickyID, DateTime? StickyDate, string Description)
        {
            return SelectListItem(StickyID, StickyDate, Description);
        }
        public List<StickyEntity> SelectListItem(Guid StickyID, DateTime? StickyDate, string Description, int? Page, int? Row, out int Count, HashSet<string> SortClause)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from table in db.StickyEntities select table);
                if (!StickyID.Equals(Guid.Empty))
                    query = query.Where(table => table.StickyID.Equals(StickyID));
                if (StickyDate.HasValue)
                    query = query.Where(table => table.StickyDate.Equals(StickyDate));
                if (!Description.Equals(String.Empty))
                    query = query.Where(table => table.Description.Equals(Description));
                for (int i = SortClause.Count - 1; i >= 0; i--)
                    query = query.OrderBy(SortClause.ElementAt(i));
                Count = query.Count();
                if (Page.HasValue && Row.HasValue)
                    return query.PageIQuery(Page.Value, Row.Value).ToList();
                return query.ToList();
            }
        }
        #endregion
    }
}
